#!/usr/bin/make -f

SHELL					:= /bin/bash
#DEB_SHLIBDEPS_INCLUDE	:= $(shell octave-config -p OCTLIBDIR) /usr/lib/atlas \
#						$(CURDIR)/src/shogun $(CURDIR)/debian/libshogun11/usr/lib
DEB_SHLIBDEPS_INCLUDE	:= /usr/lib/atlas \
						$(CURDIR)/src/shogun $(CURDIR)/debian/libshogun11/usr/lib
DEB_COMPRESS_EXCLUDE	:= .py .html .R .m .sg .lua .rb .cs .java
R_LIBRARY_DIR			:= /usr/lib/R/site-library
LIBSHOGUN				:= libshogun11
PYINTERFACES			:= python_modular python_static 
INTERFACES				:= lua_modular ruby_modular java_modular \
	csharp_modular cmdline_static \
	r_static elwms_static
#csharp_modular octave_modular cmdline_static octave_static
DEB_PYTHON2_MODULE_PACKAGES := shogun-python-static shogun-python-modular

include /usr/share/cdbs/1/rules/debhelper.mk
#include /usr/share/octave/debian/defs.make

PYVER		= $(shell pyversions -vr debian/control)
PYSTEST		= $(MAKE) -C shogun-python_static-$(pyver)/interfaces/python_static tests \
			DESTDIR=$(CURDIR)/debian/shogun-python-modular;
PYMTEST		= $(MAKE) -C shogun-python_modular-$(pyver)/interfaces/python_modular tests \
			DESTDIR=$(CURDIR)/debian/shogun-python-modular;
PYSINSTALL	= $(MAKE) -C shogun-python_static-$(pyver)/interfaces/python_static install \
			DESTDIR=$(CURDIR)/debian/shogun-python-static; \
			rm -rf $(CURDIR)/debian/shogun-python-static/usr/include; \
			rm -rf $(CURDIR)/debian/shogun-python-static/usr/lib/lib*.*;
PYMINSTALL	= $(MAKE) -C shogun-python_modular-$(pyver)/interfaces/python_modular install \
			DESTDIR=$(CURDIR)/debian/shogun-python-modular; \
			rm -rf $(CURDIR)/debian/shogun-python-modular/usr/include; \
			rm -rf $(CURDIR)/debian/shogun-python-modular/usr/lib/lib*.*;

CONFIGURE_OPTIONS= --disable-cpudetection --prefix=/usr \
		--enable-glpk --enable-readline --disable-svm-light \
		--enable-hdf5 --enable-json --enable-xml --enable-snappy \
		--enable-lzo --enable-gzip --enable-bzip2 --enable-lzma \
		--cc=clang --cxx=clang++

common-configure-arch:: debian/stamp-configure
common-configure-indep:: debian/stamp-configure

# configure
configure:: debian/stamp-configure
debian/stamp-configure:
	# create copies for each interface / python version
	set -x; $(foreach IF, $(INTERFACES), \
		cp -a src shogun-$(IF);)
	set -x; $(foreach pyver, $(PYVER), \
		for IF in $(PYINTERFACES); do cp -a src shogun-$${IF}-$(pyver); done; )
	# configure
	set -x; $(foreach IF, $(INTERFACES), \
		(cd shogun-$(IF) && ./configure $(CONFIGURE_OPTIONS) --interfaces=$(IF)) ;)
	set -x; $(foreach pyver, $(PYVER), (for IF in $(PYINTERFACES); \
		do ( cd shogun-$${IF}-$(pyver) && ./configure $(CONFIGURE_OPTIONS) --python=python$(pyver) --interfaces=$${IF}); done); )
	cd src; ./configure $(CONFIGURE_OPTIONS) --interfaces=
	cd shogun-elwms_static; ./configure $(CONFIGURE_OPTIONS) --interfaces=elwms_static,python_static,r_static
	#cd shogun-elwms_static; ./configure $(CONFIGURE_OPTIONS) --interfaces=elwms_static,python_static,r_static,octave_static
	touch $@

common-build-arch:: configure debian/stamp-build-arch
debian/stamp-build-arch:
	$(MAKE) -C src
	set -x; $(foreach IF, $(INTERFACES), $(MAKE) -C shogun-$(IF);)
	set -x; $(foreach pyver, $(PYVER), (for IF in $(PYINTERFACES); \
		do $(MAKE) -C shogun-$${IF}-$(pyver); done);)
	touch $@

install/shogun-doc-en:: debian/stamp-install-doc-en
debian/stamp-install-doc-en:
	$(MAKE) -C doc doc
	$(RM) doc/html/*.map
	$(RM) doc/html/*.md5
	$(RM) doc/html/installdox
	$(RM) doc/html/formula.repository
	mkdir -p $(CURDIR)/debian/shogun-doc-en/usr/share/doc/shogun-doc-en
	cp -r doc/html $(CURDIR)/debian/shogun-doc-en/usr/share/doc/shogun-doc-en/
	touch $@

install/shogun-doc-cn:: debian/stamp-install-doc-cn
debian/stamp-install-doc-cn:
	$(MAKE) -C doc doc
	$(RM) doc/html_cn/*.map
	$(RM) doc/html_cn/*.md5
	$(RM) doc/html_cn/installdox
	$(RM) doc/html_cn/formula.repository
	mkdir -p $(CURDIR)/debian/shogun-doc-cn/usr/share/doc/shogun-doc-cn
	cp -r doc/html_cn $(CURDIR)/debian/shogun-doc-cn/usr/share/doc/shogun-doc-cn/html
	touch $@

#libshogun
install/$(LIBSHOGUN):: debian/stamp-install-$(LIBSHOGUN)
debian/stamp-install-$(LIBSHOGUN):
	mkdir -p $(CURDIR)/debian/$(LIBSHOGUN)
	$(MAKE) -C src/shogun install \
		DESTDIR=$(CURDIR)/debian/$(LIBSHOGUN) 
	cp -p src/shogun/*.so.* $(CURDIR)/debian/$(LIBSHOGUN)/usr/lib
	$(RM) -r $(CURDIR)/debian/$(LIBSHOGUN)/usr/include
	$(RM) $(CURDIR)/debian/$(LIBSHOGUN)/usr/lib/libshogun.so
	$(RM) $(CURDIR)/debian/$(LIBSHOGUN)/usr/lib/libshogun.a
	touch $@

#libshogun-dev
install/libshogun-dev:: debian/stamp-install-libshogun-dev
debian/stamp-install-libshogun-dev:
	mkdir -p $(CURDIR)/debian/libshogun-dev
	$(MAKE) -C src/shogun install \
		DESTDIR=$(CURDIR)/debian/libshogun-dev
	$(RM) -r $(CURDIR)/debian/libshogun-dev/usr/include/shogun/ui
	$(RM) $(CURDIR)/debian/libshogun-dev/usr/lib/libshogun.so.*
	touch $@

## octave-modular
#install/shogun-octave-modular:: debian/stamp-install-octave-modular
#debian/stamp-install-octave-modular:
#	$(MAKE) -C shogun-octave_modular/interfaces/octave_modular install \
#		DESTDIR=$(CURDIR)/debian/shogun-octave-modular 
#	octave-depends
#	#$(MAKE) -C build-octave-modular tests \
#		DESTDIR=$(CURDIR)/debian/shogun-octave-modular
#	touch $@

# csharp-modular
install/shogun-csharp-modular:: debian/stamp-install-csharp-modular
debian/stamp-install-csharp-modular:
	$(MAKE) -C shogun-csharp_modular/interfaces/csharp_modular install \
		DESTDIR=$(CURDIR)/debian/shogun-csharp-modular 
	#dh_makeclilibs -a -V
	#dh_installcligac -a
	touch $@

binary-predeb/shogun-csharp-modular:: binary-fixup/$(LIBSHOGUN)
	dh_clideps -pshogun-csharp-modular

# java-modular
install/shogun-java-modular:: debian/stamp-install-java-modular
debian/stamp-install-java-modular:
	$(MAKE) -C shogun-java_modular/interfaces/java_modular install \
		DESTDIR=$(CURDIR)/debian/shogun-java-modular 
	touch $@

# ruby-modular
install/shogun-lua-modular:: debian/stamp-install-ruby-modular
debian/stamp-install-ruby-modular:
	$(MAKE) -C shogun-ruby_modular/interfaces/ruby_modular install \
		DESTDIR=$(CURDIR)/debian/shogun-ruby-modular 
	touch $@

# lua-modular
install/shogun-lua-modular:: debian/stamp-install-lua-modular
debian/stamp-install-lua-modular:
	$(MAKE) -C shogun-lua_modular/interfaces/lua_modular install \
		DESTDIR=$(CURDIR)/debian/shogun-lua-modular 
	touch $@

# python-modular
install/shogun-python-modular:: debian/stamp-install-python-modular
debian/stamp-install-python-modular: modular=-modular
debian/stamp-install-python-modular:
	set -x; $(foreach pyver, $(PYVER), $(PYMINSTALL))
	#set -x; $(foreach pyver, $(PYVER), $(PYTEST))
	dh_python2 -pshogun-python-modular
	dh_numpy
	touch $@

#cmdline-static
install/shogun-cmdline-static:: debian/stamp-install-cmdline-static
debian/stamp-install-cmdline-static:
	mkdir -p $(CURDIR)/debian/shogun-cmdline-static
	$(MAKE) -C shogun-cmdline_static/interfaces/cmdline_static install \
		DESTDIR=$(CURDIR)/debian/shogun-cmdline-static
	touch $@

##octave-static
#install/shogun-octave-static:: debian/stamp-install-octave-static
#debian/stamp-install-octave-static:
#	dh_install -pshogun-octave-static shogun-octave_static/interfaces/octave_static/sg.oct $(OCTDIR)
#	octave-depends
#	#$(MAKE) -C build-octave tests DESTDIR=$(CURDIR)/debian/shogun-octave-static
#	touch $@


# python-static
install/shogun-python-static:: debian/stamp-install-python-static
debian/stamp-install-python-static: modular=
debian/stamp-install-python-static:
	set -x; $(foreach pyver, $(PYVER), $(PYSINSTALL))
	#set -x; $(foreach pyver, $(PYVER), $(PYTEST))
	dh_python2 -pshogun-python-static
	dh_numpy
	touch $@

# elwms-static
install/shogun-elwms-static:: debian/stamp-install-elwms-static
debian/stamp-install-elwms-static:
	$(MAKE) DESTDIR=$(CURDIR)/debian/shogun-elwms-static RDIR=/usr/lib/R/site-library -C shogun-elwms_static/interfaces/elwms_static install 
	dh_python2 -pshogun-elwms-static --no-guessing-versions
	dh_numpy
	#octave-depends
	touch $@

# r-static
install/shogun-r-static:: debian/stamp-install-r-static
debian/stamp-install-r-static:
	$(MAKE) -C shogun-r_static/interfaces/r_static install \
			DESTDIR=$(CURDIR)/debian/shogun-r-static \
			RDIR=/usr/lib/R/site-library
	#$(MAKE) -C shogun-r_static tests \
			DESTDIR=$(CURDIR)/debian/shogun-r-static \
			RDIR=/usr/lib/R/site-library;
	touch $@

binary-post-install/shogun-dbg:: debian/stamp-post-install-shogun-dbg
debian/stamp-post-install-shogun-dbg:
	rm -rf $(CURDIR)/debian/shogun-dbg/usr/lib/debug/usr/lib/pyshared
	touch $@

get-orig-source:
	uscan --rename --force-download --pasv
	@echo successfully retrieved upstream tarball

clean::
	$(RM) debian/stamp-*
	$(RM) debian/shogun
	find debian -mindepth 2 -name 'shogun-*' -delete
	rm -rf shogun-*
	touch $@
